import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from project_utils.get_original_materials import get_exists_materials
import json
from project_utils.get_ac_mappings import mappings
from config import *
from concat_sql.ac_column_mapping import insert_ac_mapping
import datetime
from base_class.base_utils import spend_time
from base_class.inspect_logging import print_to_log
from aws_model.aws_stepfunc import send_task_success, send_task_failure
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED

# material_model = get_material(material_table)
mappings = mappings(history=False)


@spend_time("ac_daily2ods 耗时：")
def ac_daily2ods(event):
    table_name = sys._getframe().f_code.co_name
    if 'is_hour' in event and event['is_hour'] == 'true':
        source_table = ac_hbase_table_hour
        is_hour = True
    else:
        source_table = ac_hbase_table
        is_hour = False
    date = event.get('p_date')
    if not date:
        today = datetime.datetime.today()
        # 昨天
        date = (today - datetime.timedelta(days=1)).strftime('%Y-%m-%d')

    # 读取orginal现有materia
    materials = get_exists_materials(source_table, date)

    with ThreadPoolExecutor(4) as pool:
        results = [pool.submit(insert_ac_mapping, is_hour, mappings, date, i) for i in materials]
        wait(results, return_when=ALL_COMPLETED)

    output = {"msg": f"{table_name} submit ok", "Payload": {}}
    output = json.dumps(output)
    step_response = send_task_success(tasktoken, output)
    print_to_log("step_response: ", step_response)


if __name__ == '__main__':
    try:
        event = json.loads(sys.argv[1])
        tasktoken = event.get('TaskToken')
        ac_daily2ods(event)
    except Exception as e:
        print_to_log("任务 失败")
        print_to_log("原因： ", str(e))
        send_task_failure(str(e), f'执行{sys._getframe().f_code.co_filename}错误', tasktoken)
